/**
 *
 */

var app = getApp()
Page({
	data: {
		root:'',
		siteroot:app.setting.siteroot,
		att_url:app.setting.att_url,
		member:[],
		avatar:'',
		pagetitle:'修改头像' + '-' + app.setting.title,
		bannerHeight: Math.ceil(290.0 / 750.0 * getApp().screenWidth)
	},
	onPullDownRefresh: function (){
		var the=this;
		var opt={};
		the.onLoad(opt);
		setTimeout(function(){
			wx.stopPullDownRefresh()
		},1000);
	},
	onLoad: function (options){
		var that =this
		wx.setNavigationBarTitle({'title':that.data.pagetitle});
		app.util.get(app.access,function(res){
			if (!res.errno) {
			  that.setData({
					pagedata: res,
			  })
			}
		});
	},
	choose_file:function(e){
		var the =this
		var modaldata=the.data.modaldata?the.data.modaldata:{}
		if(modaldata.params){
			var title= modaldata.params.title?modaldata.params.title:'';
		}else{
			var title='';
		}
		modaldata.type=modaldata.type?modaldata.type:'one';
		modaldata.source_type=modaldata.source_type?modaldata.source_type:'image';
		if(e && e.currentTarget && e.currentTarget.dataset){
			var data=e.currentTarget.dataset;
			var op=data.op?data.op:'';
			var page=data.page?data.page:1;
			var group_id=data.group_id?data.group_id:'';
			if(!group_id && modaldata.params && modaldata.params.group_id){
				group_id=modaldata.params.group_id;
			}
			if(data.file){
				modaldata.filename=data.file;
			}
			if(data.tovalue){
				modaldata.tovalue=data.tovalue;
			}
			if(data.thumb){
				modaldata.thumbname=data.thumb;
			}
			modaldata.type=data.type?data.type:'one';
			modaldata.source_type=data.source_type?data.source_type:modaldata.source_type;
		}else{
			var op=modaldata.op?modaldata.op:'list';
			var group_id=modaldata.params.group_id?modaldata.params.group_id:-2;
			var page=1;
		}
		var action='core/tools.choose_file//type='+modaldata.type+'&op='+op+'&source_type='+modaldata.source_type+'&group_id='+group_id+'&title='+title+'&page='+page
		app.util.get(action,function(res){
			modaldata.file_list=modaldata.file_list?modaldata.file_list:[];
			modaldata=Object.assign(modaldata,res);
			the.setData({
				modaldata: modaldata,
				showchoosefile:true,
			})
		});
	},
	choose_file_tabop:function(e){
		var the =this
		var modaldata =the.data.modaldata
		var data=e.currentTarget.dataset;
		the.setData({
			'modaldata.op': data.op,
		})
	},
	choose_file_splice:function(e){
		var the =this
		var modaldata =the.data.modaldata
		var data=e.currentTarget.dataset;
		modaldata.file_list.splice(data.index,1);
		the.setData({
			'modaldata.file_list': modaldata.file_list,
		})
	},
	choose_file_keyword: function (e) {
		var the = this
		the.setData({
		  'modaldata.params.title': e.detail.value
		})
	},
	choose_file_image: function (e) {
		var the = this
		var modaldata =the.data.modaldata
		var pagedata =the.data.pagedata
		var att_url=the.data.att_url
		var file=modaldata.file;
		var thumb=modaldata.thumb?modaldata.thumb:'';
		if(e && e.currentTarget && e.currentTarget.dataset && e.currentTarget.dataset.index>=0){
			var data=e.currentTarget.dataset;
			var file=modaldata.list[data.index].attachment;
			var thumb=modaldata.list[data.index].thumb;
			thumb=thumb?thumb:'';
		}
		if(modaldata.type=='more'){
			if(modaldata.tovalue){
						pagedata[modaldata.tovalue][modaldata.filename].push(file);
			}else{
						pagedata[modaldata.filename].push(file);
			}
			the.setData({
					  pagedata: pagedata,
			})
		}else{
			if(file && modaldata.filename){
				    if(modaldata.tovalue){
						pagedata[modaldata.tovalue][modaldata.filename]=att_url+file
					}else{
						pagedata[modaldata.filename]=att_url+file
					}					
					if(modaldata.thumbname){
						if(modaldata.tovalue){
							pagedata[modaldata.tovalue][modaldata.thumbname]=thumb
						}else{
							pagedata[modaldata.thumbname]=thumb
						}
					}
					the.setData({
					  pagedata: pagedata,
					})
			}
		}
		the.setData({
				   showchoosefile:false,
		})		
	},
	choose_file_images:function(){
		var the = this
		var modaldata =the.data.modaldata
		var res=';'+modaldata.file_list.join(';')+';';
		the.setData({
			'file_list': res,
		})
	},
	up_file:function(){
		var the =this
		var modaldata =the.data.modaldata	
		app.util.up_file(modaldata.source_type,function(res,thumb){
			modaldata.file=res
			if(thumb){
				modaldata.thumb=thumb
			}
			the.setData({
				modaldata: modaldata,
			})	
		});
	},
	//提交订单
    formSubmit:function(e){
		var data=e.detail.value;
        var that = this;
		var pagedata =that.data.pagedata
		pagedata.item.nickname=data.nickname
        wx.showToast({
            title: '加载中',
            icon: 'loading',
            duration: 1000,
            mask:true
        });
		app.util.post('post',pagedata.item);
    },

})